using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Profile;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ClickSocial.Framework;
using System.IO;

namespace ClickSocial.Admin.controles
{
    public partial class NewUser : BaseUser
    {
        public event EventHandler UserCreated;

        virtual protected void Page_Load(object sender, EventArgs e)
        {
            if (Context.User.IsInRole("Host"))
            {
                UserName.ReadOnly = false;
                RegularExpressionValidator2.Visible = true;
                chkIsLocked.Visible = true;
                chkIsApproved.Visible = true;

                Password.Visible = true;
                ConfirmPassword.Visible = true;
                Question.Visible = true;
                Answer.Visible = true;

                PasswordLabel.Visible = true;
                ConfirmPasswordLabel.Visible = true;
                QuestionLabel.Visible = true;
                AnswerLabel.Visible = true;

                PasswordRequired.Visible = true;
                ConfirmPasswordRequired.Visible = true;
                QuestionRequired.Visible = true;
                AnswerRequired.Visible = true;

            }
        }

        protected override void btnGuardar_Click(object sender, EventArgs e)
        {
            base.btnGuardar_Click(sender, e);

            try
            {
                //guardo usuario
                DataSourceControls.UserDataSource.Insert(UserName.Text, (chkIsApproved.Visible == false ? true : chkIsApproved.Checked), Email.Text, Question.Text, Password.Text, Answer.Text);
                //guardo perfil
                PerfilComun perfil = (PerfilComun)ProfileBase.Create(UserName.Text);
                perfil.Nombre = txtNombre.Text;
                perfil.Apellido = txtApellido.Text;
                perfil.Telefono = txtTelefono.Text;
                perfil.Celular = txtCelular.Text;
                perfil.Direccion = txtDireccion.Text;
                perfil.Ciudad = txtCiudad.Text;
                perfil.Provincia = txtPcia.Text;
                perfil.Pais = txtPais.Text;

                if (ddlRoles.SelectedValue == "Fotografo")
                {
                    //Crear directorio de usuario
                    string BaseDirUser = string.Format("{0}\\{1}", Application["BaseDir"], UserName.Text);
                    if (Directory.CreateDirectory(BaseDirUser) == null) throw new ApplicationException("No se ha podido crear el directorio home del fotografo.");
                    
                    perfil.UsaPrecio = chkPrecio.Checked;
                    long res = 0;

                    if (long.TryParse(txtEspacio.Text, out res))
                    {
                        res = res * 1024 * 1024;
                    }
                    else
                    {
                        res = 50 * 1024 * 1024;
                    }
                    perfil.Espacio = res;
                    
                }
                perfil.Save();

                //guardo rol
                if (ddlRoles.Visible == true)
                {
                    Roles.AddUserToRole(UserName.Text, ddlRoles.SelectedValue);
                }

                UserCreated(sender, e);
                
            }
            catch (Exception ex)
            {
                ErrorMessage.Text = ex.Message;
            }

        }

        public void ResetControls()
        {
            UserName.Text = string.Empty;
            Password.Text = string.Empty;
            ConfirmPassword.Text = string.Empty;
            Email.Text = string.Empty;
            Question.Text = string.Empty;
            Answer.Text = string.Empty;

            if (Context.User.IsInRole("Host"))
            {                
                ListItem item = ddlRoles.Items.FindByValue("Fotografo");
                if (item != null) ddlRoles.SelectedIndex = ddlRoles.Items.IndexOf(item);
            }
            HostCambiaVisibilidad();
        }
        
    }
}